package com.rabbit.videoplayer.widget.gesture;

import android.app.Activity;
import android.view.View;

import com.rabbit.videoplayer.dialog.gesturedialog.BrightnessDialog;
import com.rabbit.videoplayer.dialog.gesturedialog.VolumeDialog;

/*
 * Copyright (C) 2010-2018 Alibaba Group Holding Limited.
 */

/**
 * 手势对话框的管理器。
 */

public class GestureDialogManager {

    //用于构建手势用的dialog
    private Activity mActivity;
    //当前屏幕默认为小屏
    private boolean isFullScreen;
    //音量对话框
    private VolumeDialog mVolumeDialog = null;
    //亮度对话框
    private BrightnessDialog mBrightnessDialog = null;

    public GestureDialogManager(Activity activity) {
        mActivity = activity;
    }

    /**
     * 显示亮度对话框
     * @param parent 显示在哪个view中间
     */
    public void showBrightnessDialog(View parent,int currentBrightness) {
        if (mBrightnessDialog == null) {
            mBrightnessDialog = new BrightnessDialog(mActivity, currentBrightness);
        }
        if (!mBrightnessDialog.isShowing()) {
            mBrightnessDialog.setScreenMode(isFullScreen);
            mBrightnessDialog.show(parent);
            mBrightnessDialog.updateBrightness(currentBrightness);
        }
    }

    /**
     * 更新亮度值
     * @param changePercent 亮度变化百分比
     * @return 最终的亮度百分比
     */
    public int updateBrightnessDialog(int changePercent) {
        int targetBrightnessPercent = mBrightnessDialog.getTargetBrightnessPercent(changePercent);
        mBrightnessDialog.updateBrightness(targetBrightnessPercent);
        return targetBrightnessPercent;
    }

    /**
     * 隐藏亮度对话框
     */
    public void dismissBrightnessDialog() {
        if (mBrightnessDialog != null && mBrightnessDialog.isShowing()) {
            mBrightnessDialog.dismiss();
        }
        mBrightnessDialog = null;
    }

    public void initDialog(Activity activity){
        this.mActivity = activity;
        if (mVolumeDialog == null) {
            mVolumeDialog = new VolumeDialog(activity);
        }
    }

    /**
     * 显示音量对话框
     * @param parent  显示在哪个view中间
     * @param currentPercent 当前音量百分比
     */
    public void showVolumeDialog(View parent, float currentPercent) {
        if (mVolumeDialog == null) {
            mVolumeDialog = new VolumeDialog(mActivity);
        }
        if (!mVolumeDialog.isShowing()) {
            mVolumeDialog.setScreenMode(isFullScreen);
            mVolumeDialog.show(parent);
        }
        mVolumeDialog.updateVolume(currentPercent);
    }

    public boolean isVolumeDialogIsShow(){
        if(mVolumeDialog == null){
            return false;
        }
        return mVolumeDialog.isShowing();
    }

    /**
     * 更新音量
     * @param changePercent 变化的百分比
     * @return 最终的音量百分比
     */
    public float updateVolumeDialog(int changePercent) {
        float targetVolume = mVolumeDialog.getTargetVolume(changePercent);
        mVolumeDialog.updateVolume(targetVolume);
        return targetVolume;
    }
    public float getCurrentVolume() {
        if (mVolumeDialog == null) {
            mVolumeDialog = new VolumeDialog(mActivity);
        }
        return mVolumeDialog.currentVolume;
    }
    /**
     * 关闭音量对话框
     */
    public void dismissVolumeDialog() {
        if (mVolumeDialog != null && mVolumeDialog.isShowing()) {
            mVolumeDialog.dismiss();
        }
        mVolumeDialog = null;
    }

    public void setCurrentScreenMode(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
    }
}
